package com.jdbc.guigu.student;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

import org.junit.Test;
/**
 * 使用面向对象编程的思想，多层方法实现，只需在一个测试方法中调用即可。
 * @author Enginner-Jie
 */
public class StudentTest {
	@Test//查询测试
	public void testGetStudent() {
		// 1. 得到查询的类型
		int searchType = getSearchType();

		// 2. 具体查询学生信息
		Student student = searchStudent(searchType);

		// 3. 打印学生信息
		if (student != null) {
			System.out.println(student);
		} else {
			System.out.println("查无此人!");
		}
	}
	@Test//插入记录测试
	public void testAddNewStudent() {
		// 1.得到学生信息
		Student student = getStudentFromConsole();
		// 2.执行添加方法
		addNewStudent(student);
	}
	/**
	 * 添加记录
	 * @param student
	 */
	public void addNewStudent(Student student) {
		String sql = "INSERT INTO examstudent(type, idcard, "
				+ "examcard, studentname, location, grade) "
				+ "VALUES(?,?,?,?,?,?)";

		JDBCTools.update(sql,student.getType(),
				student.getIdCard(), student.getExamCard(),
				student.getStudentName(), student.getLocation(),
				student.getGrade());
	}

	/**
	 * 具体查询学生信息的. 返回一个 Student对象. 若不存在, 则返回 null
	 * @param searchType: 1 或 2.
	 * @return Student
	 */
	private Student searchStudent(int searchType) {

		String sql = "SELECT flowid, type, idcard, examcard,"
				+ "studentname, location, grade " + "FROM examstudent "
				+ "WHERE ";
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);

		if (searchType == 1) {
			System.out.print("请输入身份证号:");
			String idCard = scanner.next();
			sql = sql + "idcard = '" + idCard + "'";
		} else {
			System.out.print("请输入准考证号:");
			String examCard = scanner.next();
			sql = sql + "examcard = '" + examCard + "'";
		}
		Student student = getStudent(sql);
		//把查询结果封装为一个 Student 对象
		return student;
	}
	
	/**
	 * 查询根据传入的 SQL 返回 Student 对象
	 * @param sql
	 * @return Student
	 */
	private Student getStudent(String sql) {

		Student stu = new Student();

		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);

			if (resultSet.next()) {
				stu.setFlowId(resultSet.getInt(1));
				stu.setType(resultSet.getInt(2));
				stu.setIdCard(resultSet.getString(3));
				stu.setExamCard(resultSet.getString(4));
				stu.setStudentName(resultSet.getString(5));
				stu.setLocation(resultSet.getString(6));
				stu.setGrade(resultSet.getInt(7));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.releaseDB(resultSet, statement, connection);
		}
		return stu;
	}
	
	/**
	 * 从控制台读入一个整数, 确定要查询的类型
	 * @return: 1. 身份证. 2. 准考证号.
	 */
	private int getSearchType() {
		System.out.print("请输入查询类型: 1. 用身份证查询. 2. 用准考证号查询 ");
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		int type = scanner.nextInt();

		if (type != 1 && type != 2) {
			System.out.println("输入有误请重新输入!");
			throw new RuntimeException();
		}
		return type;
	}
	/**
	 * 从控制台输入学生的信息
	 */
	private Student getStudentFromConsole() {

		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		Student student = new Student();

		System.out.print("Type: ");
		student.setType(scanner.nextInt());

		System.out.print("IdCard:");
		student.setIdCard(scanner.next());

		System.out.print("ExamCard:");
		student.setExamCard(scanner.next());

		System.out.print("StudentName:");
		student.setStudentName(scanner.next());

		System.out.print("Location:");
		student.setLocation(scanner.next());

		System.out.print("Grade:");
		student.setGrade(scanner.nextInt());

		return student;
	}
}
